package com.mach.platform.sprider.dev;


import com.mach.platform.sprider.Conf;
import com.mach.platform.util.SqlHelper;
import org.joda.time.DateTime;

import java.io.IOException;

public class CompessAirDev extends DataHelper {

    protected String devName;

    protected SqlHelper sqls;

    public CompessAirDev(String ip, Integer port,Integer devID, String devName) {
        super(ip,port,devID);

        this.sqls  = Conf.getSqlHelper();

        this.devName = devName;
    }

    @Override
    public void run(){

        long preSec = 0;
        long nowSec = 0;
        while(true){


            try {

//                long nowMill = System.currentTimeMillis();
//
//                nowSec = nowMill / 1000;
//
//                if(nowSec == preSec)continue;
//
//                Date now = new Date(nowMill);
//
//                int fyear = now.getYear() + 1900;
//                int fmonth = now.getMonth() + 1;
//                int fday = now.getDate();
//                int fhour = now.getHours();
//                int fmin = now.getMinutes();
//                int fsec = now.getSeconds();

                DateTime dateTime = new DateTime();
                int fyear = dateTime.getYear();
                int fmonth = dateTime.getMonthOfYear();
                int fday = dateTime.getDayOfMonth();
                int fhour = dateTime.getHourOfDay();
                int fmin = dateTime.getMinuteOfHour();
                int fsec = dateTime.getSecondOfMinute();

                Float rlFlow =  this.getCpAirRealTimeFlow();
                Integer sum = this.getCpAirSumFlow();

                try {
                    this.sqls.updateAsync("insert ignore into data_detail(dev_id, fyear, fmonth, fday, fhour, fmin, fsec,fdatetime, " +
                            "ca_real_flow,ca_sum_flow) value" +
                            "('"+this.devName+"',"+fyear+","+fmonth+","+fday+","+fhour+","+fmin+","+fsec+","+"date_format(concat("+fyear+",'-',"+fmonth+",'-',"+fday+",' ',"+fhour+",':',"+fmin+",':',"+fsec+")," +
                            "'%Y-%m-%d %H:%i:%s'),"
                            +rlFlow+","+sum+")");
                } catch (Exception e) {
                    e.printStackTrace();
                }

                try {
                    this.sqls.updateAsync("replace into data_cur (dev_id,ca_real_flow,ca_sum_flow,update_time) value " +
                            "( " +
                            "'"+this.devName+"',"+
                            rlFlow + ","+
                            sum + "," +
                            "date_format(concat("+fyear+",'-',"+fmonth+",'-',"+fday+",' ',"+fhour+",':',"+fmin+",':',"+fsec+")," +
                                    "'%Y-%m-%d %H:%i:%s')" +
                            ")");
                } catch (Exception e) {
                    e.printStackTrace();
                }


            } catch (Exception e) {
                e.printStackTrace();
            }

            preSec = nowSec;


        }


    }

    /*
      压缩空气：实时流量
   */
    public Float getCpAirRealTimeFlow() throws IOException {
        Float result = null;

        DataHelper dh = this;

        byte[] repData = dh.getDataRtu(6,2);
        if(repData.length <1)return null;
        byte[] data = ByteUtil.subData(repData,3,4);
        ByteUtil.reveIn2Byte(data, 0);
        ByteUtil.reveIn2Byte(data, 2);
        result = ByteUtil.getFloat(data);

        return result;
    }

    /*
        压缩空气：累积流量
     */
    public Integer getCpAirSumFlow() throws IOException {
        Integer result = null;

        DataHelper dh = this;

        byte[] repData = dh.getDataRtu(12,2);
        if(repData.length <1)return null;
        byte[] data = ByteUtil.subData(repData,3,4);
        data = ByteUtil.reverse2Byte(data);
        data = ByteUtil.reverse(data);
        result = new Integer(ByteUtil.getInt(data));

        return result;
    }

    public static void main(String[] args) throws Exception{
        CompessAirDev dev = new CompessAirDev("192.168.1.201",30001,2,"");
        Object value = dev.getCpAirRealTimeFlow();

        System.out.println("value="+value);
    }
}
